class Admin::UsersController < AdminController
  def index
    @users = User.all
    if params[:search_key].presence
      @users = @users.where("phone LIKE ?", "%#{params[:search_key]}%")
    end
    @users = @users.order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def show
    @user = User.find params[:id]
  end

  def edit
    @user = User.find params[:id]
  end
  
  def update
    @user = User.find params[:id]
    if @user.update user_params
      redirect_to admin_users_path, notice: "修改成功."
    else
      redirect_to admin_users_path, notice: "修改失败."
    end
  end
  
  def destroy
    @user = User.find params[:id]
    if @user.destroy
      redirect_to admin_users_path, notice: "删除成功."
    else
      redirect_to admin_users_path, notice: "删除失败."
    end
  end

  private

  def user_params
    params.require(:user).permit(:state, :moneybag, :membership_id, :bonus)
  end
  
end
